package com.knowledgegraph.neo4j.controller;

import com.knowledgegraph.common.Result;
import com.knowledgegraph.neo4j.entity.GraphRelation;
import com.knowledgegraph.neo4j.service.Neo4jRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Neo4j 关系控制器
 */
@RestController
@RequestMapping("/neo4j/relations")
@RequiredArgsConstructor
public class Neo4jRelationController {
    
    private final Neo4jRelationService neo4jRelationService;
    
    /**
     * 创建关系
     */
    @PostMapping
    public Result<GraphRelation> createRelation(@RequestBody Map<String, Object> relationData) {
        Long sourceId = Long.valueOf(relationData.get("sourceId").toString());
        Long targetId = Long.valueOf(relationData.get("targetId").toString());
        String type = relationData.get("type").toString();
        String description = relationData.get("description").toString();
        Double strength = Double.valueOf(relationData.get("strength").toString());
        Double confidence = Double.valueOf(relationData.get("confidence").toString());
        Long graphId = Long.valueOf(relationData.get("graphId").toString());
        String creator = relationData.get("creator").toString();
        
        GraphRelation created = neo4jRelationService.createRelation(sourceId, targetId, type, description,
                strength, confidence, graphId, creator);
        return Result.success(created);
    }
    
    /**
     * 更新关系
     */
    @PutMapping("/{id}")
    public Result<GraphRelation> updateRelation(@PathVariable Long id, @RequestBody Map<String, Object> relationData) {
        String type = relationData.get("type").toString();
        String description = relationData.get("description").toString();
        Double strength = Double.valueOf(relationData.get("strength").toString());
        Double confidence = Double.valueOf(relationData.get("confidence").toString());
        
        GraphRelation updated = neo4jRelationService.updateRelation(id, type, description, strength, confidence);
        return Result.success(updated);
    }
    
    /**
     * 删除关系
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteRelation(@PathVariable Long id) {
        neo4jRelationService.deleteRelation(id);
        return Result.success();
    }
    
    /**
     * 根据知识图谱ID查询关系
     */
    @GetMapping("/graph/{graphId}")
    public Result<List<Map<String, Object>>> getRelationsByGraphId(@PathVariable Long graphId) {
        List<Map<String, Object>> relations = neo4jRelationService.findByGraphId(graphId);
        return Result.success(relations);
    }
    
    /**
     * 根据关系类型查询
     */
    @GetMapping("/type/{type}")
    public Result<List<Map<String, Object>>> getRelationsByType(@PathVariable String type) {
        List<Map<String, Object>> relations = neo4jRelationService.findByType(type);
        return Result.success(relations);
    }
    
    /**
     * 查询两个实体之间的关系
     */
    @GetMapping("/between")
    public Result<List<GraphRelation>> getRelationsBetween(@RequestParam Long sourceId, @RequestParam Long targetId) {
        List<GraphRelation> relations = neo4jRelationService.findRelationsBetween(sourceId, targetId);
        return Result.success(relations);
    }
    
    /**
     * 获取关系统计信息
     */
    @GetMapping("/statistics/{graphId}")
    public Result<Map<String, Object>> getRelationStatistics(@PathVariable Long graphId) {
        Map<String, Object> statistics = neo4jRelationService.getRelationStatistics(graphId);
        return Result.success(statistics);
    }
    
    /**
     * 获取关系类型分布
     */
    @GetMapping("/type-distribution/{graphId}")
    public Result<List<Map<String, Object>>> getRelationTypeDistribution(@PathVariable Long graphId) {
        List<Map<String, Object>> distribution = neo4jRelationService.getRelationTypeDistribution(graphId);
        return Result.success(distribution);
    }
    
    /**
     * 查询实体的所有关系
     */
    @GetMapping("/entity/{entityId}")
    public Result<List<Map<String, Object>>> getEntityRelations(@PathVariable Long entityId) {
        List<Map<String, Object>> relations = neo4jRelationService.getEntityRelations(entityId);
        return Result.success(relations);
    }
    
    /**
     * 查询强关系
     */
    @GetMapping("/strong/{graphId}")
    public Result<List<Map<String, Object>>> getStrongRelations(@PathVariable Long graphId,
                                                               @RequestParam(defaultValue = "0.8") Double threshold) {
        List<Map<String, Object>> relations = neo4jRelationService.getStrongRelations(graphId, threshold);
        return Result.success(relations);
    }
    
    /**
     * 查询知识图谱结构
     */
    @GetMapping("/structure/{graphId}")
    public Result<Map<String, Object>> getGraphStructure(@PathVariable Long graphId) {
        Map<String, Object> structure = neo4jRelationService.getGraphStructure(graphId);
        return Result.success(structure);
    }
    
    /**
     * 查询最短路径
     */
    @GetMapping("/shortest-path")
    public Result<List<Map<String, Object>>> findShortestPath(@RequestParam Long startId, @RequestParam Long endId) {
        List<Map<String, Object>> path = neo4jRelationService.findShortestPath(startId, endId);
        return Result.success(path);
    }
    
    /**
     * 批量创建关系
     */
    @PostMapping("/batch")
    public Result<List<GraphRelation>> batchCreateRelations(@RequestBody List<Map<String, Object>> relationData) {
        List<GraphRelation> created = neo4jRelationService.batchCreateRelations(relationData);
        return Result.success(created);
    }
}